// 轮播图功能
document.addEventListener('DOMContentLoaded', function() {
    // 轮播图功能增强
    const slider = document.querySelector('.slider');
    const slides = document.querySelectorAll('.slide');
    const prevBtn = slider.querySelector('.prev');
    const nextBtn = slider.querySelector('.next');
    const dotsContainer = slider.querySelector('.slider-dots');
    let currentSlide = 0;
    let slideInterval = null;

    // 初始化轮播图
    function initSlider() {
        // 设置初始位置
        slides.forEach((slide, index) => {
            slide.style.transform = `translateX(${index * 100}%)`;
        });

        // 创建轮播点
        slides.forEach((_, index) => {
            const dot = document.createElement('div');
            dot.classList.add('dot');
            if (index === 0) dot.classList.add('active');
            dot.addEventListener('click', () => goToSlide(index));
            dotsContainer.appendChild(dot);
        });
    }

    // 切换到指定幻灯片
    function goToSlide(index) {
        slides.forEach((slide, i) => {
            slide.style.transform = `translateX(${(i - index) * 100}%)`;
        });
        currentSlide = index;
        updateDots();
    }

    // 更新轮播点状态
    function updateDots() {
        const dots = dotsContainer.querySelectorAll('.dot');
        dots.forEach((dot, index) => {
            dot.classList.toggle('active', index === currentSlide);
        });
    }

    // 下一张幻灯片
    function nextSlide() {
        currentSlide = (currentSlide + 1) % slides.length;
        goToSlide(currentSlide);
    }

    // 上一张幻灯片
    function prevSlide() {
        currentSlide = (currentSlide - 1 + slides.length) % slides.length;
        goToSlide(currentSlide);
    }

    // 开始自动轮播
    function startAutoSlide() {
        if (slideInterval) clearInterval(slideInterval);
        slideInterval = setInterval(nextSlide, 5000);
    }

    // 停止自动轮播
    function stopAutoSlide() {
        if (slideInterval) {
            clearInterval(slideInterval);
            slideInterval = null;
        }
    }

    // 初始化轮播图
    initSlider();
    startAutoSlide();

    // 事件监听
    prevBtn.addEventListener('click', () => {
        prevSlide();
        stopAutoSlide();
        startAutoSlide();
    });

    nextBtn.addEventListener('click', () => {
        nextSlide();
        stopAutoSlide();
        startAutoSlide();
    });

    // 鼠标悬停时暂停自动轮播
    slider.addEventListener('mouseenter', stopAutoSlide);

    // 鼠标离开时恢复自动轮播
    slider.addEventListener('mouseleave', startAutoSlide);

    // 触摸事件支持
    let touchStartX = 0;
    let touchEndX = 0;

    slider.addEventListener('touchstart', (e) => {
        touchStartX = e.touches[0].clientX;
        stopAutoSlide();
    }, { passive: true });

    slider.addEventListener('touchmove', (e) => {
        touchEndX = e.touches[0].clientX;
    }, { passive: true });

    slider.addEventListener('touchend', () => {
        const difference = touchStartX - touchEndX;
        if (Math.abs(difference) > 50) { // 最小滑动距离
            if (difference > 0) {
                nextSlide();
            } else {
                prevSlide();
            }
        }
        startAutoSlide();
    });

    // 商品分类标签切换
    const tabs = document.querySelectorAll('.tab');
    tabs.forEach(tab => {
        tab.addEventListener('click', function() {
            tabs.forEach(t => t.classList.remove('active'));
            this.classList.add('active');
            // 这里可以添加筛选商品的逻辑
        });
    });

    // 购物车功能增强
    const addToCartBtns = document.querySelectorAll('.add-to-cart');
    addToCartBtns.forEach(btn => {
        btn.addEventListener('click', function(e) {
            e.preventDefault();
            const card = this.closest('.product-card');
            const product = {
                id: card.dataset.id,
                name: card.querySelector('h3').textContent,
                price: parseFloat(card.querySelector('.current').textContent.slice(1)),
                image: card.querySelector('.product-img img').src
            };
            cart.addItem(product);
            showNotification('商品已添加到购物车');
        });
    });
});

// 通知提示功能
function showNotification(message) {
    const notification = document.createElement('div');
    notification.classList.add('notification');
    notification.textContent = message;
    document.body.appendChild(notification);

    setTimeout(() => {
        notification.classList.add('show');
        setTimeout(() => {
            notification.classList.remove('show');
            setTimeout(() => {
                notification.remove();
            }, 300);
        }, 2000);
    }, 100);
}

// 购物车功能
class ShoppingCart {
    constructor() {
        this.items = JSON.parse(localStorage.getItem('cart')) || [];
    }

    addItem(product) {
        const existingItem = this.items.find(item => item.id === product.id);
        if (existingItem) {
            existingItem.quantity += 1;
        } else {
            this.items.push({...product, quantity: 1});
        }
        this.saveCart();
    }

    removeItem(productId) {
        this.items = this.items.filter(item => item.id !== productId);
        this.saveCart();
    }

    saveCart() {
        localStorage.setItem('cart', JSON.stringify(this.items));
    }
}

// 初始化购物车
const cart = new ShoppingCart(); 

// 登录注册功能
document.addEventListener('DOMContentLoaded', function() {
    const loginModal = document.getElementById('loginModal');
    const registerModal = document.getElementById('registerModal');
    const loginBtn = document.getElementById('loginBtn');
    const registerBtn = document.getElementById('registerBtn');
    const showRegister = document.getElementById('showRegister');
    const showLogin = document.getElementById('showLogin');
    const closeBtns = document.querySelectorAll('.close');

    // 打开模态框
    function openModal(modal) {
        modal.style.display = 'block';
    }

    // 关闭模态框
    function closeModal(modal) {
        modal.style.display = 'none';
    }

    // 切换模态框
    function switchModal(closeThis, openThat) {
        closeModal(closeThis);
        openModal(openThat);
    }

    // 事件监听
    loginBtn.addEventListener('click', () => openModal(loginModal));
    registerBtn.addEventListener('click', () => openModal(registerModal));
    showRegister.addEventListener('click', () => switchModal(loginModal, registerModal));
    showLogin.addEventListener('click', () => switchModal(registerModal, loginModal));

    // 关闭按钮
    closeBtns.forEach(btn => {
        btn.addEventListener('click', () => {
            closeModal(loginModal);
            closeModal(registerModal);
        });
    });

    // 点击模态框外部关闭
    window.addEventListener('click', (e) => {
        if (e.target === loginModal) closeModal(loginModal);
        if (e.target === registerModal) closeModal(registerModal);
    });

    // 登录表单提交
    document.getElementById('loginForm').addEventListener('submit', function(e) {
        e.preventDefault();
        const username = document.getElementById('loginUsername').value;
        const password = document.getElementById('loginPassword').value;

        // 这里添加登录逻辑
        console.log('登录:', { username, password });
        
        // 模拟登录成功
        showNotification('登录成功');
        closeModal(loginModal);
        updateUserStatus(username);
    });

    // 注册表单提交
    document.getElementById('registerForm').addEventListener('submit', function(e) {
        e.preventDefault();
        const username = document.getElementById('regUsername').value;
        const email = document.getElementById('regEmail').value;
        const password = document.getElementById('regPassword').value;
        const confirmPassword = document.getElementById('regConfirmPassword').value;

        if (password !== confirmPassword) {
            showNotification('两次输入的密码不一致', 'error');
            return;
        }

        // 这里添加注册逻辑
        console.log('注册:', { username, email, password });

        // 模拟注册成功
        showNotification('注册成功');
        closeModal(registerModal);
        // 自动切换到登录框
        setTimeout(() => openModal(loginModal), 1000);
    });

    // 更新用户状态
    function updateUserStatus(username) {
        const userText = document.querySelector('.user-text');
        const guestMenu = document.querySelector('.guest-menu');
        const userMenuLogged = document.querySelector('.user-menu-logged');
        
        userText.textContent = username;
        guestMenu.style.display = 'none';
        userMenuLogged.style.display = 'block';
        
        // 保存登录状态
        localStorage.setItem('currentUser', username);

        // 添加退出登录功能
        document.getElementById('logoutBtn').addEventListener('click', function(e) {
            e.preventDefault();
            // 清除登录状态
            localStorage.removeItem('currentUser');
            userText.textContent = '登录/注册';
            guestMenu.style.display = 'block';
            userMenuLogged.style.display = 'none';
        });
    }

    // 检查登录状态
    function checkLoginStatus() {
        const username = localStorage.getItem('currentUser');
        if (username) {
            updateUserStatus(username);
        }
    }

    // 页面加载时检查登录状态
    checkLoginStatus();
}); 